import os, sys, re

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'riscv_build_dataset.settings')

import django

django.setup()

from mongo_log import OBSLog
from obs.models import obs_package_build_log, obs_package, obs_job_history
from obs.spider.package_build_log_spider import log_status_test
from tqdm import tqdm

o = OBSLog()

logs = o.get_all_log(4241)


def init_opbl():
    packages = obs_package.objects.all()

    for p in tqdm(packages):
        package_id = p.id
        logs = o.get_all_log(package_id)
        if logs:
            opbl = []
            for l in logs:
                try:
                    fetch_time = l.uploadDate
                    log_id = l._id
                    text = l.read().decode('utf-8')
                    last_line = text.split('\n')[-3]
                    status = ''
                    if 'failed "build' in last_line:
                        status = 'failed'
                    elif 'finished "build' in last_line:
                        status = 'succeeded'
                    else:
                        status = 'unknown'
                    t = obs_package_build_log(
                        project=p.project,
                        package=p,
                        fetch_time=fetch_time,
                        build_log_id=log_id,
                        status=status
                    )
                    opbl.append(t)
                except Exception as e:
                    print(e, file=sys.stderr)
            obs_package_build_log.objects.bulk_create(opbl, ignore_conflicts=True)


def update_opbl():
    logs = obs_package_build_log.objects.filter(status='unknown')
    for l in tqdm(logs):
        _id = l.build_log_id
        file = o.get_log_by_id(_id)
        if file:
            text = file.read().decode('utf-8')
            new_status = log_status_test(text)
            if new_status != 'unknown':
                l.status = new_status
                l.save()
            # print(log_status_test(text))

def update_jh_to_mongo_id():
    packages = obs_package.objects.all()

    for p in tqdm(packages):
        package_id = p.id
        logs = o.get_all_log(package_id)
        if logs:
            for l in logs:
                try:
                    pattern = r"srcmd5 '[0-9a-f]{32}'"
                    text = l.read().decode('utf-8')
                    find = re.search(pattern, text)
                    if find:
                        srcmd5 = find.group().partition("'")[2][:-1]
                        # print(srcmd5)

                        job_hists = obs_job_history.objects.filter(srcmd5=srcmd5)
                        for j in job_hists:
                            j.mongo_log_id = l._id
                            j.save()
                        # print(job_hist)
                except Exception as e:
                    print(e, file=sys.stderr)

if __name__ == '__main__':
    # update_opbl()
    update_jh_to_mongo_id()
